import asyncio
from pyppeteer import launch

width, height = 1366, 768


async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.setViewport({'width': width, 'height': height})  # 调用pyppeteer 的 API setViewport 完成窗口大小设置
    await page.goto('https://dynamic2.scrape.center/')
    await page.waitForSelector('.item .name')
    await asyncio.sleep(2)
    await page.screenshot(path=r'截图.png')  # 页面截图
    # 调用了 evaluate 方法执行了一些 JavaScript方法，返回图片
    dimensions = await page.evaluate('''() => {   
       return {
           width: document.documentElement.clientWidth,
           height: document.documentElement.clientHeight,
           deviceScaleFactor: window.devicePixelRatio,
       }
   }''')

    print(dimensions)
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())
